// ============================= [Introduction] ==============================
// Soj.me: problem #1151
// by David Qiu. <david@davidqiu.com>
//
// Optimizations:
//    1. Storage of magic block from size 8 array to an single integer;
//    2. Hex representation of the magic block to allow bit operation;
//    3. Use Cantor expension to cut the branches;
//    4. Cache from Cantor base; 
//
// ================================= [END] ===================================

//#define DEBUG

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstddef>
#include <queue> 
using namespace std;


struct MagicBlock
{
  unsigned int blocks;
  
  MagicBlock()
  {
    blocks = (unsigned int)5
           + ((unsigned int)6 << 4)
           + ((unsigned int)7 << 8)
           + ((unsigned int)8 << 12)
           + ((unsigned int)4 << 16)
           + ((unsigned int)3 << 20)
           + ((unsigned int)2 << 24)
           + ((unsigned int)1 << 28); 
  } 
  
  // Set the magic blocks.
  // The order is as follow:
  //    0 1 2 3
  //    4 5 6 7
  //
  // @Param arr: the initialization array.
  // @Return: void
  void set(unsigned int* arr)
  {
    blocks = (unsigned int)arr[7]
           + ((unsigned int)arr[6] << 4)
           + ((unsigned int)arr[5] << 8)
           + ((unsigned int)arr[4] << 12)
           + ((unsigned int)arr[3] << 16)
           + ((unsigned int)arr[2] << 20)
           + ((unsigned int)arr[1] << 24)
           + ((unsigned int)arr[0] << 28); 
  }
  
  // Get the corresponding block from the blocks.
  // The index is as follow:
  //    7 6 5 4
  //    3 2 1 0
  //
  // @Param index: the index of the block
  // @Return: the corresponding number of the block
  unsigned int get(int index)
  {
    return (blocks >> (index * 4)) & 0xf;
  }
  
  // Operation A:
  //    Switch the upper and lower lines.
  //    e.g.
  //        1 2 3 4    ->    8 7 6 5
  //        8 7 6 5          1 2 3 4
  //
  // @Param:  void
  // @Return: void
  void opA()
  {
    blocks = ((blocks & 0xffff) << 16) + (blocks >> 16);
  }
  
  // Operation B:
  //    Shift right for each line.
  //    e.g.
  //        1 2 3 4    ->    4 1 2 3
  //        8 7 6 5          5 8 7 6
  //
  // @Param:  void
  // @Return: void
  void opB()
  {
    unsigned int line1 = blocks, line2 = blocks;
    
    line1 >>= 4;
    line1 = (((line1 & 0xf000) << 16) + line1) & 0xffff0000;
    
    line2 &= 0xffff;
    line2 = (((line2 & 0xf) << 16) + line2) >> 4;
    
    blocks = line1 + line2;
  }
  
  // Operation C:
  //    Center turns clockwise for one block.
  //    e.g.
  //        1 2 3 4    ->    1 7 2 4
  //        8 7 6 5          8 6 3 5
  //
  // @Param:  void
  // @Return: void
  void opC()
  {
    unsigned int a=blocks, b=blocks, c=blocks, d=blocks;
    
    a &= 0x0f000000;
    a >>= 4;
    
    b &= 0x00f00000;
    b >>= 16;
    
    c &= 0xf0;
    c <<= 4;
    
    d &= 0xf00;
    d <<= 16;
    
    blocks = (blocks & 0xf00ff00f) + a + b + c + d;
  }
  
  #ifdef DEBUG
  // Print the magic block on screen.
  // It is a debug function.
  //
  // @Param: void
  // @Return: void
  void print()
  {
    for(int i=7;i>=0;--i)
    {
      printf("%d ", get(i));
      if(i==4||i==0) printf("\n");
    }
  }
  #endif
};


// The factorial table from 0 ~ 12.
const unsigned int factorial[13] = {1, 
    1, 2, 6, 24, 
    120, 720, 5040, 40320, 
    362880, 3628800, 39916800, 479001600};


class CantorBase
{
public:
  CantorBase()
  {
    _count = 8;
    _size = factorial[_count] + 1;
    _base = new string[_size];
    for(int i=0;i<_size;++i) _base[i] = "-";
    
    #ifdef DEBUG
    cout << "CantorBase.size: " << _size << endl; 
    #endif
  }
  
  /*
  ~CantorBase()
  {
    delete _base;
    _base = NULL;
  }
  */
  
  // Check a corresponding place for the path to a desired magic block.
  //
  // @Param mb: the desired magic block to lookup.
  // @Return: a string is return to indicate if the corresponding path.
  //          If the path is not filled, a "-" is returned.
  string lookup(MagicBlock& mb)
  {
    return _base[CantorExpend(mb)];
  }
  
  // Update the corresponding path to a magic block.
  //
  // @Param mb: the index magic block.
  // @Param ops: the corresponding operation path.
  // @Return: void
  void update(MagicBlock& mb, string ops)
  {
    _base[CantorExpend(mb)] = ops;
  }
  
private:
  size_t _count;
  size_t _size;
  string* _base;
  
  size_t CantorExpend(MagicBlock& mb)
  {
    unsigned int arr[8]; // 7 6 5 4 ; 3 2 1 0 ;
    for(int i=0;i<8;++i) arr[i] = mb.get(i);
    size_t ce = 0;
    
    int count;
    for(int i=7;i>=1;--i)
    {
      count = 0;
      for(int j=i-1;j>=0;--j) if(arr[i]>arr[j]) ++count;
      ce += count * factorial[i];
    }
    
    return ce;
  }
};



struct QNode
{
    MagicBlock mb;
    string ops;// = "";
    
    QNode() { ops = ""; }
};


CantorBase cb; // Cantor base to store the temperate results
queue<QNode> q; // The search queue
QNode qn; // The temperate queue node
bool init = false; // Initialization state

string find(MagicBlock origin, MagicBlock desire, int maxSteps)
{
    // Check for initialization of the first finding round
    if(!init)
    {
      // Initialize the search queue
      qn.mb = origin;
      q.push(qn);
      
      // The origin magic block implies no operation, cache it 
      cb.update(qn.mb, "");
      
      // Initialized
      init = true;
    }
    
    // The result operation string
    string rops = cb.lookup(desire);
    
    // If cached result found, return directly
    if(rops!="-")
    {
      if(rops.length()<=maxSteps) return rops;
      else return "X";
    }
    
    // If not cached data, continue the search
    while(true)
    {
        if(q.front().ops.length()>=maxSteps)
        {
          return "X";
        }
        
        // Try operation A
        qn = q.front();
        qn.mb.opA();
        if(cb.lookup(qn.mb)=="-") // Cache not found
        {
            qn.ops += "A"; // Append operation
            q.push(qn); // Push into the search queue
            cb.update(qn.mb, qn.ops); // Cache the node
            if(qn.mb.blocks == desire.blocks) return qn.ops;
        }
        
        // Try operation B 
        qn = q.front();
        qn.mb.opB();
        if(cb.lookup(qn.mb)=="-") // Cache not found
        {
            qn.ops += "B"; // Append operation
            q.push(qn); // Push into the search queue
            cb.update(qn.mb, qn.ops); // Cache the node
            if(qn.mb.blocks == desire.blocks) return qn.ops;
        }
        
        // Try operation C 
        qn = q.front();
        qn.mb.opC();
        if(cb.lookup(qn.mb)=="-") // Cache not found
        {
            qn.ops += "C"; // Append operation
            q.push(qn); // Push into the search queue
            cb.update(qn.mb, qn.ops); // Cache the node
            if(qn.mb.blocks == desire.blocks) return qn.ops;
        }
        
        q.pop();
    }
}



int main()
{
    int steps;
    unsigned int reader[8];
    MagicBlock mbOri, mbDes;
    
    while(cin>>steps && steps>-1)
    {
        // Input desired magic block state
        for(int i=0;i<8;++i) scanf("%d", reader+i);
        mbDes.set(reader);
        
        // Calculate the path
        string res = find(mbOri, mbDes, steps);
        if(res == "X")
        {
            cout << -1 << endl;
            continue;
        }
        cout << res.length() << " " << res << endl;
    }
    
    return 0;
}

